package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;

import butterknife.BindView;
import retrofit2.Call;

public class ChangePasswordActivity extends BaseActivity{

    @BindView(R.id.old_password)
    EditText old_password;
    @BindView(R.id.new_password01)
    EditText new_password01;
    @BindView(R.id.new_password02)
    EditText new_password02;
    private Context context = this;
    @Override
    protected int getLayoutResId() {
        return R.layout.ui_change_password;
    }

    @Override
    protected void initView() {
        findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(old_password.getText()) || TextUtils.isEmpty(new_password01.getText()) || TextUtils.isEmpty(new_password02.getText())){
                    showToast("密码都不能为空");
                    return;
                }
                if (!new_password01.getText().toString().equals(new_password02.getText().toString())){
                    ToastUtils.toastShort("2次密码输入不一致！");
                    return;
                }
                editPassword();
            }
        });
    }

    private void editPassword() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().editPassword(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), old_password.getText().toString(), new_password01.getText().toString());
        call.enqueue(new OnResponseListener<ResultObjectModel>(this) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
